home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / strfield / strfield.bas < prev    next >
BASIC Source File  |  1994-07-07  |  2KB  |  61 lines

  1.  
  2.  
  3. Function strfield$ (record$, separator$, fieldnumber%)
  4.  
  5.  
  6.       'I must yield glory to Pete Thompson, creator of the Lotus Ami Pro Macro Language,
  7.       'for the idea to create the StrField function which is present in the Ami Pro
  8.       'Macro Language, but ABSENT in Visual Basic.  The StrField function has three
  9.       'parameters:
  10.       '
  11.       '     1. a string which is a record of fields delimited by a specific character
  12.       '     2. the delimiter
  13.       '     3. the field number to be extracted from the record
  14.  
  15.  
  16.       If Len(record$) = 0 Then               'return null if zero length string
  17.          strfield$ = ""
  18.          Exit Function
  19.       End If
  20.  
  21.       If InStr(record$, separator$) = 0 Then 'return null if less than two fields
  22.          strfield$ = ""
  23.          Exit Function
  24.       End If
  25.  
  26.       If fieldnumber% < 1 Then               'return null if requested field number is not positive
  27.          strfield$ = ""
  28.          Exit Function
  29.       End If
  30.  
  31.       For a = 1 To Len(record$)              'count separators for next evaluation
  32.          b = Mid$(record$, a, 1)
  33.             If b = separator$ Then
  34.                y = y + 1
  35.             End If
  36.       Next a
  37.  
  38.       If y < (fieldnumber% - 1) Then         'return null if non-existent field is requested
  39.          strfield$ = ""
  40.          Exit Function
  41.       End If
  42.  
  43.       For x = 1 To Len(record$)              'parse requested field from record
  44.          c = Mid$(record$, x, 1)
  45.             If c = separator$ Then
  46.                z = z + 1
  47.                   If z = (fieldnumber% - 1) Then
  48.                      record$ = Right$(record$, Len(record$) - x)
  49.                   End If
  50.             End If
  51.       Next x
  52.  
  53.       If InStr(record$, separator$) = 0 Then 'if no separators left in record, then remaining record IS the
  54.          strfield$ = record$                 'requested field (last)
  55.       Else
  56.          strfield$ = Left$(record$, InStr(record$, separator$) - 1)
  57.       End If
  58.  
  59. End Function
  60.  
  61.